﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using System.Diagnostics;

namespace _2DGame
{
    public class Player
    {
        bool Hasplayed = false;
        bool Hasplayed2 = false;
        #region Property
        Texture2D _PlayerGraphics;
        Vector2 _playerCord;

        int _Life;//Stores the amount of lifes the cat have.

        double hastighet = 0;
        double tid = 0;
        double tid2 = 0;
        double gravitationsAcceleration;
        //Number of time the player has jumped.
        int keyState = 0;
        float x = 0;
        //Do prevent the contiues jump and double jump to merga.
        int pressCounter = 0;
        //the position on the Y cord you want the player to start in. (to match the background)
        float PlayerStartY = 40.0f;
        public Texture2D PlayerGraphics
        {
            get { return _PlayerGraphics; }
            set { _PlayerGraphics = value; }
        }
        public Vector2 PlayerCord
        {
            get { return _playerCord; }
            set { _playerCord = value; }
        }
        public int Life
        {
            get { return _Life; }
            set { _Life = value; }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="height">The height of the screen, altertive the game window.</param>
        public Player(float height, int life, Texture2D texture2D)
        {
            PlayerGraphics = texture2D;
            _Life = life;
            _playerCord = new Vector2(10.0f, height - PlayerStartY - PlayerGraphics.Height);
            gravitationsAcceleration = 200 * LevelController.SpeedRunner();
        }
        /// <summary>
        /// Draw the player on the screen.
        /// </summary>
        /// <param name="spriteBatch">The Spirebatch from the game to draw up the player.</param>
        /// <param name="NumberOfJump">Number of jumps the player has done for two jumps 2.</param>
        public void DrawPlayer(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(_PlayerGraphics, _playerCord, Color.White);
            spriteBatch.End();
        }
        #region Jump
        /// <summary>
        /// a simple jump and falling back to the ground.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="gameTime"></param>
        public void SimpleJump(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            if (keyState == 1)
            {
                Vector2 Temp = new Vector2(PlayerCord.X, (float)(hastighet * tid + gravitationsAcceleration * tid * tid / 2) + graphics.GraphicsDevice.Viewport.Height - PlayerGraphics.Height - PlayerStartY);
                PlayerCord = Temp;
                tid = tid + gameTime.ElapsedGameTime.TotalSeconds;
                //So you know were the double jump started.
                if (tid2 < 0.1)
                {
                    x = PlayerCord.Y;
                }
            }
        }
        /// <summary>
        /// if the player double press space when the player starts to fall back this will happen.
        /// </summary>
        /// <param name="gameTime"></param>
        public void DoubleJump(GameTime gameTime)
        {
            //number of ms before you can double jump.
            if (keyState == 2 && pressCounter < 60)
            {
                Vector2 Temp = new Vector2(PlayerCord.X, (float)(hastighet * tid2 + gravitationsAcceleration * tid2 * tid2 / 2) + x);
                PlayerCord = Temp;
                tid2 = tid2 + gameTime.ElapsedGameTime.TotalSeconds;
                //To prevent contiues jump during double jump.
                if (pressCounter < 40)
                {
                    pressCounter = 0;
                }
            }
        }
        /// <summary>
        /// Start the event of jumping when the player press down the space key.
        /// </summary>
        public void start()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                keyState = 1;
                hastighet = -200;
                pressCounter += 1;
            }
        }
        /// <summary>
        /// When the player hit the bottem this will happen. 
        /// </summary>
        /// <param name="graphics"></param>
        private void JumpFinish(GraphicsDeviceManager graphics)
        {
            if (PlayerCord.Y > graphics.GraphicsDevice.Viewport.Height - PlayerGraphics.Height - PlayerStartY)
            {
                Vector2 Temp = new Vector2(PlayerCord.X, graphics.GraphicsDevice.Viewport.Height - PlayerGraphics.Height);
                _playerCord.X = Temp.X;
                _playerCord.Y = Temp.Y - 40;
                keyState = 0;
                tid = 0;
                tid2 = 0;
                pressCounter = 0;
                gravitationsAcceleration = 200;

                if (tid == 0)//check if jump is done
                {
                    Hasplayed = false;//set to false so can play jump sound again
                    if (Keyboard.GetState().IsKeyUp(Keys.Space))
                    {
                        Hasplayed2 = false;//set to false so can play jump sound again
                    }

                }
            }
        }
        public void StopFalling(GraphicsDeviceManager graphics, float Y)
        {
            Vector2 Temp = new Vector2(PlayerCord.X, graphics.GraphicsDevice.Viewport.Height - PlayerGraphics.Height);
            _playerCord.X = Temp.X;
            _playerCord.Y = Y - 53;
            keyState = 0;
            tid = 0;
            tid2 = 0;
            pressCounter = 0;
            gravitationsAcceleration = 200;


            if (tid == 0)//check if jump is done
            {
                Hasplayed = false;//set to false so can play jump sound again
                if (Keyboard.GetState().IsKeyUp(Keys.Space))
                {
                    Hasplayed2 = false;//set to false so can play jump sound again
                }

            }
        }
        public void ResetPlayer(GraphicsDeviceManager graphics)
        {
            //Fixa gravitationen vid fall från sten.
            Vector2 Temp = new Vector2(PlayerCord.X, (float)(hastighet * tid + gravitationsAcceleration * tid / 2) + graphics.GraphicsDevice.Viewport.Height - PlayerGraphics.Height - PlayerStartY);
            _playerCord = Temp;
            keyState = 0;
            tid = 0;
            tid2 = 0;
            pressCounter = 0;
            gravitationsAcceleration = 200;

            if (tid == 0)//check if jump is done
            {
                Hasplayed = false;//set to false so can play jump sound again
                if (Keyboard.GetState().IsKeyUp(Keys.Space))
                {
                    Hasplayed2 = false;//set to false so can play jump sound again
                }

            }
        }
        /// <summary>
        /// Make the player jump if the key is pressed.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="gameTime"></param>
        public void Jump(GraphicsDeviceManager graphics, GameTime gameTime, SoundEffect JumpSound)// added inparameter for sound
        {

            start();
            if (Keyboard.GetState().IsKeyUp(Keys.Space))
            {
                pressCounter = 0;
            }

            SimpleJump(graphics, gameTime);

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && tid > 1)
            {
                keyState = 2;

                if (Keyboard.GetState().IsKeyDown(Keys.Space) && Hasplayed2 == false)// check if key is pressed secound time and sound has been playd
                {
                    JumpSound.Play(1F, 0.8F, 0F);//Play jump sound with higher pitch
                    Hasplayed2 = true;
                }
            }
            DoubleJump(gameTime);
            JumpFinish(graphics);
            if (Keyboard.GetState().IsKeyDown(Keys.Space) && Hasplayed == false)// check if key is pressed and sound has been playd
            {
                JumpSound.Play(1F, 0.7F, 0F);
                Hasplayed = true;
            }

        }
    }
        #endregion
}
